package com.sina.oefund;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.nio.charset.Charset;

import com.sina.util.StringUtil;

/**
 * Created by IntelliJ IDEA.
 * User: qtang
 * Date: Jul 4, 2013
 * Time: 1:53:07 PM
 * To change this template use File | Settings | File Templates.
 */
public class SinaOEFundClient {

    private final static String FUND_URL = "http://api.sinaapp.org/?code=";
    private final static String CHARSET_GBK = "gbk";
    private final static String CHARSET_GB2312 = "gb2312";
    private final static String CHARSET_UTF8 = "UTF-8";

	private final static int CONNECTION_TIMEOUT = 5000;
	private final static int SO_TIMEOUT = 30000;

	private HttpClient mHttpClient;
	private static SinaOEFundClient mInstance;

    private SinaOEFundClient(){
		mHttpClient = new HttpClient();
		mHttpClient.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
		mHttpClient.getHttpConnectionManager().getParams().setSoTimeout(SO_TIMEOUT);
	}

	/**
	 *
	 * @return SinaStockClient
	 */
	public synchronized static SinaOEFundClient getInstance() {
		if(mInstance != null) {
			return mInstance;
		}
		return new SinaOEFundClient();
	}

	/**
	 * @throws IOException
	 * @throws HttpException
	 * @throws SinaOEFund.ParseStockInfoException
	 */
	public List<SinaOEFund> getFundList(String[] stockCodes) throws IOException, SinaOEFund.ParseStockInfoException {
		String url = FUND_URL + StringUtil.transferArray2String(stockCodes);
		HttpMethod method = new GetMethod(url);
		int statusCode = mHttpClient.executeMethod(method);
		if(statusCode != HttpStatus.SC_OK) {
			method.releaseConnection();
			return null;
		}
		InputStream is = method.getResponseBodyAsStream();
		InputStreamReader reader = new InputStreamReader(new BufferedInputStream(is), Charset.forName(CHARSET_UTF8));
		BufferedReader bReader = new BufferedReader(reader);
		List<SinaOEFund> list = parseSinaOEFundListFromReader(bReader);
		bReader.close();
		method.releaseConnection();
		return list;
	}

    private List<SinaOEFund> parseSinaOEFundListFromReader(BufferedReader reader) throws IOException, SinaOEFund.ParseStockInfoException {
		ArrayList<SinaOEFund> list = new ArrayList<SinaOEFund>(10);
		String sourceLine = null;
		while((sourceLine = reader.readLine()) != null) {
//            System.out.println(sourceLine);
			list.add(SinaOEFund.parseFund(sourceLine));
		}
		return list;
	}

	//��ȡ֤ȯ��Ϣ
//	public Map<String,SinaOEFund> getStockInfoMap(String[] stockCodes) throws IOException, SinaOEFund.ParseStockInfoException {
//		String url = STOCK_URL + StringUtil.transferArray2String(stockCodes);
//		HttpMethod method = new GetMethod(url);
//		int statusCode = mHttpClient.executeMethod(method);
//		if(statusCode != HttpStatus.SC_OK) {
//			method.releaseConnection();
//			return null;
//		}
//		InputStream is = method.getResponseBodyAsStream();
//		InputStreamReader reader = new InputStreamReader(new BufferedInputStream(is), Charset.forName("gbk"));
//		BufferedReader bReader = new BufferedReader(reader);
//
//		Map<String,SinaOEFund> sMap = new HashMap<String,SinaOEFund>();
//		String sourceLine = null;
//		int i=0;
//		while((sourceLine = bReader.readLine()) != null) {
//			sMap.put(stockCodes[i++], SinaOEFund.parseFund(sourceLine));
//		}
//		bReader.close();
//		method.releaseConnection();
//		return sMap;
//	}

    public static void main(String[] args) {
        String[] fundCodes = new String[]{"jj161812","jj163109"};
        SinaOEFundClient client = SinaOEFundClient.getInstance();
        List<SinaOEFund> fundList ;
        try {
            fundList = client.getFundList(fundCodes);

            for (SinaOEFund sinaOEFund : fundList) {
                System.out.println(sinaOEFund.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SinaOEFund.ParseStockInfoException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }


}
